﻿#include <QDebug>
#include "CVideoLargeDlg.h"
#include "CGlobal.h"
#include "Public.h"

CVideoLargeDlg::CVideoLargeDlg(CDevCamer* pDev, int nType, QWidget *parent)
	: m_pDev(pDev),m_nType(nType),QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("摄像头监控画面"));
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setWindowsTitleBarColor(hwnd, RGB(56, 63, 81));
	setStyleSheetFile();
	connect(ui.pushButtonVideoPlay_2, &QPushButton::clicked, this, &CVideoLargeDlg::slotPushPlay);
	connect(ui.pushButtonAdd, &QPushButton::pressed, this, &CVideoLargeDlg::slotPushAddPressed);
	connect(ui.pushButtonAdd, &QPushButton::released, this, &CVideoLargeDlg::slotPushAddReleased);
	connect(ui.pushButtonDel, &QPushButton::pressed, this, &CVideoLargeDlg::slotPushDelPressed);
	connect(ui.pushButtonDel, &QPushButton::released, this, &CVideoLargeDlg::slotPushDelReleased);
	connect(ui.pushButtonUp, &QPushButton::pressed, this, &CVideoLargeDlg::slotPushUpPressed);
	connect(ui.pushButtonUp, &QPushButton::released, this, &CVideoLargeDlg::slotPushUpReleased);
	connect(ui.pushButtonDown, &QPushButton::pressed, this, &CVideoLargeDlg::slotPushDownPressed);
	connect(ui.pushButtonDown, &QPushButton::released, this, &CVideoLargeDlg::slotPushDownReleased);
	connect(ui.pushButtonLeft, &QPushButton::pressed, this, &CVideoLargeDlg::slotPushLeftPressed);
	connect(ui.pushButtonLeft, &QPushButton::released, this, &CVideoLargeDlg::slotPushLeftReleased);
	connect(ui.pushButtonRight, &QPushButton::pressed, this, &CVideoLargeDlg::slotPushRightPressed);
	connect(ui.pushButtonRight, &QPushButton::released, this, &CVideoLargeDlg::slotPushRightReleased);
	connect(ui.pushButtonPtz, &QPushButton::clicked, this, &CVideoLargeDlg::slotPushPtz);
	// 方式1：无限制范围的整数
	ui.lineEditPtz->setValidator(new QIntValidator(this));
	if (m_pDev->m_pDevData->m_nVideoType == 0)
	{
		ui.pushButtonUp->hide();
		ui.pushButtonDown->hide();
		ui.pushButtonLeft->hide();
		ui.pushButtonRight->hide();
		ui.lineEditPtz->hide();
		ui.pushButtonPtz->hide();
		ui.pushButtonAdd->hide();
		ui.pushButtonDel->hide();
	}
	m_bIsPlaying = false;
	startPlayVideo();
}

CVideoLargeDlg::~CVideoLargeDlg()
{
	endPlayVideo();
}

void CVideoLargeDlg::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

bool CVideoLargeDlg::endPlayVideo()
{
	if (!m_pDev->m_pDevData->m_bLogin)
		return false;
	if (m_bIsPlaying == false)
		return true;
	SCtrInfo infoTx;
	infoTx.m_pLabel = ui.label;
	infoTx.m_hWid = ui.label->winId();
	infoTx.m_sGuid = m_pDev->m_sDevGuid;
	infoTx.m_nCtrCmd = enCStopRealPlay;
	infoTx.m_nCtrDevType = enSubDev1;
	if (m_nType == 0)
	{
		infoTx.m_nSubCtrDevType = enCamer;
	}
	else if (m_nType == 1)
	{
		infoTx.m_nSubCtrDevType = enIR;
	}
	infoTx.m_nSubCtrDevType = enCamer;
	infoTx.m_bCtrSync = false;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet == true)
	{
		m_bIsPlaying = false;
		ui.pushButtonVideoPlay_2->setIcon(QIcon(":/icon/play.png"));
	}
	return bTxRet;
}

bool CVideoLargeDlg::startPlayVideo()
{
	if (!m_pDev->m_pDevData->m_bLogin)
		return false;
	if (m_bIsPlaying)
		return true;
	SCtrInfo infoTx;
	infoTx.m_pLabel = ui.label;
	infoTx.m_hWid = ui.label->winId();
	infoTx.m_sGuid = m_pDev->m_sDevGuid;
	infoTx.m_nCtrCmd = enCRealPlay;
	infoTx.m_nCtrDevType = enSubDev1;
	if (m_nType == 0)
	{
		infoTx.m_nSubCtrDevType = enCamer;
	}
	else if (m_nType == 1)
	{
		infoTx.m_nSubCtrDevType = enIR;
	}
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet == true)
	{
		m_bIsPlaying = true;
		ui.pushButtonVideoPlay_2->setIcon(QIcon(":/icon/StopPlay.png"));
	}
	return bTxRet;
}

void CVideoLargeDlg::closeEvent(QCloseEvent* event)
{
	endPlayVideo();
	QDialog::closeEvent(event); // 调用父类处理
}

void CVideoLargeDlg::slotPushPlay()
{
	if (m_bIsPlaying)//如果正在播放视频
	{
		endPlayVideo();
		
	}
	else//则播放当前选中的节点
	{
		startPlayVideo();
		
	}
}

void CVideoLargeDlg::slotPushUpPressed()
{
	qDebug() << A2T("按下上");
	SCtrInfo info;
	info.m_nCtrCmd = enCTiltUp;
	m_pDev->ctrDev(info);
}

void CVideoLargeDlg::slotPushUpReleased()
{
	qDebug() << A2T("松开上");
	SCtrInfo info;
	info.m_nCtrCmd = enCTiltUpStop;
	m_pDev->ctrDev(info);
}

void CVideoLargeDlg::slotPushDownPressed()
{
	SCtrInfo info;
	info.m_nCtrCmd = enCTiltDown;
	m_pDev->ctrDev(info);
}

void CVideoLargeDlg::slotPushDownReleased()
{
	SCtrInfo info;
	info.m_nCtrCmd = enCTiltDownStop;
	m_pDev->ctrDev(info);
}

void CVideoLargeDlg::slotPushLeftPressed()
{
	SCtrInfo info;
	info.m_nCtrCmd = enCPanLeft;
	m_pDev->ctrDev(info);
}

void CVideoLargeDlg::slotPushLeftReleased()
{
	SCtrInfo info;
	info.m_nCtrCmd = enCPanLeftStop;
	m_pDev->ctrDev(info);
}

void CVideoLargeDlg::slotPushRightPressed()
{
	SCtrInfo info;
	info.m_nCtrCmd = enCPanRight;
	m_pDev->ctrDev(info);
}

void CVideoLargeDlg::slotPushRightReleased()
{
	SCtrInfo info;
	info.m_nCtrCmd = enCPanRightStop;
	m_pDev->ctrDev(info);
}

void CVideoLargeDlg::slotPushAddPressed()
{
	SCtrInfo info;
	info.m_nCtrCmd = enZoomAdd;
	m_pDev->ctrDev(info);
}

void CVideoLargeDlg::slotPushAddReleased()
{
	SCtrInfo info;
	info.m_nCtrCmd = enZoomAddStop;
	m_pDev->ctrDev(info);
}

void CVideoLargeDlg::slotPushDelPressed()
{
	SCtrInfo info;
	info.m_nCtrCmd = enZoomSubtract;
	m_pDev->ctrDev(info);
}

void CVideoLargeDlg::slotPushDelReleased()
{
	SCtrInfo info;
	info.m_nCtrCmd = enZoomSubtractStop;
	m_pDev->ctrDev(info);
}

void CVideoLargeDlg::slotPushPtz()
{
	SCtrInfo info;
	info.m_nCtrCmd = enRunToPrestNo;
	info.m_nCamerAngle = ui.lineEditPtz->text().toInt();
	m_pDev->ctrDev(info);
}
